import { formatDate } from './common'
import config from './config'
var md5 = require('blueimp-md5')
export function xml2json (xml) {
  // Create the return object
  var obj = {}
  if (xml.nodeType === 1) { // element
  // do attributes
    if (xml.attributes.length > 0) {
      obj['@attributes'] = {}
      for (var j = 0; j < xml.attributes.length; j++) {
        var attribute = xml.attributes.item(j)
        obj['@attributes'][attribute.nodeName] = attribute.nodeValue
      }
    }
  } else if (xml.nodeType === 3) { // text
    obj = xml.nodeValue
  }
  // do children
  if (xml.hasChildNodes()) {
    for (var i = 0; i < xml.childNodes.length; i++) {
      var item = xml.childNodes.item(i)
      var nodeName = item.nodeName
      if (typeof (obj[nodeName]) === 'undefined') {
        obj[nodeName] = xml2json(item)
      } else {
        if (typeof (obj[nodeName].push) === 'undefined') {
          var old = obj[nodeName]
          obj[nodeName] = []
          obj[nodeName].push(old)
        }
        obj[nodeName].push(xml2json(item))
      }
    }
  }
  return obj
}
/**
 * 拼接取请求国信接口的XML
 * @param {*} data
 */
export function getXMLStr (data) {
  console.log(formatDate(new Date(), 'yyyyMMddhhmmss'))
  let time = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss').replace(/-/g, '').replace(/:/g, '').replace(/ /g, '')
  let servicecode = config.GX.servicecode
  let clientcode = config.GX.clientcode
  let finalKey = config.GX.finalKey
  let ticket = md5(time + servicecode + clientcode + finalKey)
  // console.log('requestData>>>', data)
  let servername = data.servername
  let dataXML = ''
  for (var i in data.data) {
    dataXML += '<' + i + '>' + data.data[i] + '</' + i + '>'
  }
  let xmlStr =
  '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:imag="http://imageCenter.web.com">' +
    '<soapenv:Header/>' +
    '<soapenv:Body>' +
        '<imag:ImageInterfaceService soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' +
          '<param xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' +
            '<![CDATA[' +
            '<?xml version="1.0" encoding="UTF-8"?>' +
                  '<params>' +
                  '<safety>' +
                    '<clientcode>' + clientcode + '</clientcode>' +
                    '<servicecode>' + servicecode + '</servicecode>' +
                    '<time>' + time + '</time>' +
                    '<ticket>' + ticket + '</ticket>' +
                  '</safety>' +
                  '<serverbody>' +
                    '<servername>' + servername + '</servername>' +
                    '<services>' +
                    '<service>' +
                      '<serviceid>1</serviceid>' +
                      dataXML +
                    '</service>' +
                    '</services>' +
                  '</serverbody>' +
                  '</params>' +
            ']]>' +
          '</param>' +
        '</imag:ImageInterfaceService>' +
    '</soapenv:Body>' +
  '</soapenv:Envelope>'
  return xmlStr
}
